<template>
  <div class="app-container">
    <el-card style="max-width:100%;background-color:#e7eaec" >
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="150px"align="center" >
      <el-form-item label="报修人" prop="people">
        <el-input
          v-model="queryParams.people"
          placeholder="请输入报修人"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="保修日期" prop="warrantyDate">
        <el-date-picker clearable
          v-model="queryParams.warrantyDate"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择保修日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="保修内容" prop="content" >
        <el-select
            v-model="queryParams.content"
            placeholder="请选择保修内容"
            clearable
            style="width: 400px"
        >
          <el-option
              v-for="item in repairType"
              :key="item.value"
              :label="item.label"
              :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    </el-card>
    <br>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['repair:repairApplication:add']"
        >申请</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['repair:repairApplication:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['repair:repairApplication:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['repair:repairApplication:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="repairApplicationList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="保修申请编号" align="center" prop="id" />
      <el-table-column label="报修部门" align="center" prop="departmentName" />
      <el-table-column label="门牌号" align="center" prop="doorplate" />
      <el-table-column label="报修人" align="center" prop="people" />
      <el-table-column label="保修内容" align="center" prop="content">
        <template #default="scope">
          <span>{{scope.row.content.substring(1,scope.row.content.length-1)}}</span>
        </template>
      </el-table-column>
      <el-table-column label="保修日期" align="center" prop="warrantyDate" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.warrantyDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
<!--      <el-table-column label="其他问题" align="center" prop="anotherProblem" />
      <el-table-column label="详细描述" align="center" prop="description" />-->
      <el-table-column label="单据状态" align="center" prop="states" >
        <template #default="scope">
          <span v-if="scope.row.states===0" style="color: #3e6898">待接单</span>
          <span v-if="scope.row.states===1" style="color: green">已接单</span>
          <span v-if="scope.row.states===2" style="color: red">已驳回</span>
          <span v-if="scope.row.states===3" style="color: #9eacc5">已撤销</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width " width="300px">
        <template #default="scope">
          <el-button link icon="View" style="background-color:rgba(255,192,203,0.71);color:deeppink;" @click="handleSelect(scope.row)">查看</el-button>
          <el-button link icon="Refresh"  v-if="scope.row.states===2" style="color: #E6A23C;background-color: cornsilk" @click="handleStates(scope.row)">撤销</el-button>
          <el-button link icon="Edit"  style="color: #67C23A;background-color: azure" @click="handleAccept(scope.row)"
          v-if="userRole==='admin'"
          >接单</el-button>
          <el-button link icon="Delete" style="color: red;background-color:#ffe5e1" @click="handleOverrule(scope.row)" v-if="userRole==='admin'"
          >驳回</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改保修申请对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="repairApplicationRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="报修部门" prop="departmentName">
          <el-input v-model="form.departmentName" placeholder="请输入报修部门" />
        </el-form-item>
        <el-form-item label="门牌号" prop="doorplate">
          <el-input v-model="form.doorplate" placeholder="请输入门牌号" />
        </el-form-item>
        <el-form-item label="报修人" prop="people">
          <el-input v-model="form.people" placeholder="请输入报修人" />
        </el-form-item>
        <el-form-item label="保修日期" prop="warrantyDate">
          <el-date-picker clearable
            v-model="form.warrantyDate"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择保修日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="保修内容" prop="content">
          <el-checkbox-group v-model="form.content">
            <el-checkbox label="电脑">电脑</el-checkbox>
            <el-checkbox label="打印机">打印机</el-checkbox>
            <el-checkbox label="电话">电话</el-checkbox>
            <el-checkbox label="网络">网络</el-checkbox>
            <el-checkbox label="宣判系统">宣判系统</el-checkbox>
            <el-checkbox label="签章系统">签章系统</el-checkbox>
            <el-checkbox label="法庭">法庭</el-checkbox>
            <el-checkbox label="楼层复印件">楼层复印件</el-checkbox>
          </el-checkbox-group>
        </el-form-item>
        <el-form-item label="其他问题" prop="anotherProblem">
          <el-input v-model="form.anotherProblem" placeholder="请输入其他问题" />
        </el-form-item>
        <el-form-item label="详细描述" prop="description">
          <el-input v-model="form.description" placeholder="请输入详细描述" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">提交</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>

    <!-- 查看保修申请对话框 -->
    <el-dialog :title="title" v-model="openSelect" width="500px" append-to-body>
      <el-form ref="repairApplicationRef" :model="openSelect"  label-width="80px">
        <el-form-item label="报修部门" prop="departmentName">
          <el-input v-model="form.departmentName" placeholder="无" />
        </el-form-item>
        <el-form-item label="门牌号" prop="doorplate">
          <el-input v-model="form.doorplate" placeholder="无" />
        </el-form-item>
        <el-form-item label="报修人" prop="people">
          <el-input v-model="form.people" placeholder="无" />
        </el-form-item>
        <el-form-item label="保修日期" prop="warrantyDate">
          <el-date-picker clearable
                          v-model="form.warrantyDate"
                          type="date"
                          value-format="YYYY-MM-DD"
                          placeholder="无">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="保修内容" prop="content">
          <el-input v-model="form.content" placeholder="无" />
        </el-form-item>
        <el-form-item label="其他问题" prop="anotherProblem">
          <el-input v-model="form.anotherProblem" placeholder="无" />
        </el-form-item>
        <el-form-item label="详细描述" prop="description">
          <el-input v-model="form.description" placeholder="无" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="readForm">已阅读</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="RepairApplication">
import { listRepairApplication, getRepairApplication, delRepairApplication, addRepairApplication, updateRepairApplication } from "@/api/repair/repairApplication";
import {ref} from 'vue'
import {ElMessage} from "element-plus";
import result from "@/components/Crontab/result.vue";
const { proxy } = getCurrentInstance();
import useUserStore from '@/store/modules/user'
const repairApplicationList = ref([]);
const open = ref(false);
const openSelect = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const data = reactive({
  form: {
    content: ''
  },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    people: null,
    content: null,
    warrantyDate: null,
  },
  rules: {
    people: [
      { required: true, message: "报修人不能为空", trigger: "blur" }
    ],
    content: [
      { required: true, message: "保修内容不能为空", trigger: "change" }
    ],
  }
});
const userStore=useUserStore()
const userRole=userStore.roles.toString()
const { queryParams, form, rules } = toRefs(data);
/** 查询保修申请列表 */
function getList() {
  loading.value = true;
  listRepairApplication(queryParams.value).then(response => {
    repairApplicationList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    departmentName: null,
    doorplate: null,
    people: null,
    content: [],
    warrantyDate: null,
    anotherProblem: null,
    description: null,
    states: null
  };
  proxy.resetForm("repairApplicationRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加保修申请";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getRepairApplication(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改保修申请";
  });
}
/** 查看按钮操作 */
function handleSelect(row) {
  reset();
  const _id = row.id || ids.value
  getRepairApplication(_id).then(response => {
    form.value = response.data;
    openSelect.value = true;
    title.value = "查看保修申请详情";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["repairApplicationRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        // 更新保修申请
        updateRepairApplication(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        }).catch(error => {
          console.error("修改失败：", error);
        });
      } else {
        form.value.content=JSON.stringify(form.value.content)
        // 添加新的保修申请
        addRepairApplication(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        }).catch(error => {
          console.error("新增失败：", error);
        });
      }
    }
  });
}
/** 已阅读按钮 */
function readForm() {
  ElMessage.success(result.msg?result.msg:'阅读成功')
  openSelect.value = false;
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除保修申请编号为"' + _ids + '"的数据项？').then(function() {
    return delRepairApplication(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}
/*修改单据状态为已驳回*/
function handleStates(row){
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认撤销保修申请编号为"' + _ids + '"的单据？').then(function() {
    form.value.id=_ids
    form.value.states='3'
    return updateRepairApplication(form.value);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("撤销成功");
  }).catch(() => {});
}
/*修改单据状态为已接单*/
function handleAccept(row){
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认接收保修申请编号为"' + _ids + '"的单据？').then(function() {
    form.value.id=_ids
    form.value=row
    form.value.states='1'
    return updateRepairApplication(form.value);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("接单成功");
  }).catch(() => {});
}

/*修改单据状态为已驳回*/
function handleOverrule(row){
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认驳回保修申请编号为"' + _ids + '"的单据？').then(function() {
    form.value.id=_ids
    form.value.states='2'
    return updateRepairApplication(form.value);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("驳回成功");
  }).catch(() => {});
}
/** 导出按钮操作 */
function handleExport() {
  proxy.download('repair/repairApplication/export', {
    ...queryParams.value
  }, `repairApplication_${new Date().getTime()}.xlsx`)
}

getList();
//报修内容类别
const repairType=[
  {label :'电脑',value:1}
  ,{label :'打印机',value:2}
  ,{label :'电话',value:3}
  ,{label :'网络',value:4}
  ,{label :'宣判系统',value:5}
  ,{label :'签章系统',value:6}
  ,{label :'法庭',value:7}
  ,{label :'楼层复印件',value:8}
]
</script>
